<?php
class area_widget
{
    /**
    * 获取所有的地区列表
    */
    public function get_area_list()
    {   
        $area_list = array();
        //获取所有省份
        $pro_filter = array();
        $pro_filter['status'] = 1;
        $pro_filter['depth'] = 1;

        $province_list = _model('region')->getList($pro_filter);

        foreach ($province_list as $k => $province) {
            $area_list[$k]['pro_id']    = $province['area_code'];
            $area_list[$k]['province']  = $province['area'];
            $area_list[$k]['city_list'] = array();

            //获取对应省份下的所有城市
            $city_filter = array();
            $city_filter['status']   = 1;
            $city_filter['depth']    = 2;
            $city_filter['parent_id'] = $province['area_code'];

            $city_list = _model('region')->getList($city_filter);

            foreach ($city_list as $key => $city) {
                $area_list[$k]['city_list'][$key]['city_id']     = $city['area_code'];
                $area_list[$k]['city_list'][$key]['city']        = $city['area'];
                $area_list[$k]['city_list'][$key]['county_list'] = array();

                //获取对应城市下的所有的区县
                $county_filter = array();
                $county_filter['status']   = 1;
                $county_filter['depth']    = 3;
                $county_filter['parent_id'] = $city['area_code'];

                $county_list = _model('region')->getList($county_filter);

                foreach ($county_list as $kk => $county) {
                    $area_list[$k]['city_list'][$key]['county_list'][$kk]['county_id'] = $county['area_code'];
                    $area_list[$k]['city_list'][$key]['county_list'][$kk]['county'] = $county['area'];
                }
            }
        }

        return $area_list;
    }

    /**
     * 获取单条省市区的信息
     * @param 地区的id $area_id
     */
    public function get_area_info_by_area_id($area_id)
    {
        //获取这个地区id的信息
        $area_info = _uri('region', $area_id);
        
        $area_tree = array();
        //判断这个地区是省市区
        if ($area_info['depth'] == 1) {
            //获取省份信息
            $area_tree['province_info'] = array();
        	$area_tree['province_info']['province_id'] = $area_info['id'];
        	$area_tree['province_info']['province'] = $area_info['area'];
        	$area_tree['province_info']['area_code'] = $area_info['area_code'];
        } elseif($area_info['depth'] == 2) {
            //获取城市信息
            $area_tree['city_info'] = array();
            $area_tree['city_info']['city_id'] = $area_info['id'];
            $area_tree['city_info']['city'] = $area_info['area'];
            $area_tree['city_info']['area_code'] = $area_info['area_code'];
            
            //获取省份信息
            $province_info = _uri('region', $area_info['parent_id']);
            $area_tree['province_info'] = array();
        	$area_tree['province_info']['province_id'] = $province_info['id'];
        	$area_tree['province_info']['province'] = $province_info['area'];
        	$area_tree['province_info']['area_code'] = $area_info['area_code'];
        	
             
        } elseif($area_info['depth'] == 3) {
            //获取区县信息
            $area_tree['county_info'] = array();
            $area_tree['county_info']['county_id'] = $area_info['id'];
            $area_tree['county_info']['county'] = $area_info['area'];
            $area_tree['county_info']['area_code'] = $area_info['area_code'];
            
            //获取城市信息
            $city_info = _uri('region', $area_info['parent_id']);
            $area_tree['city_info'] = array();
            $area_tree['city_info']['city_id'] = $city_info['id'];
            $area_tree['city_info']['city'] = $city_info['area'];
            $area_tree['city_info']['area_code'] = $city_info['area_code'];

            //获取省份信息
            $province_info = _uri('region', $city_info['parent_id']);
            $area_tree['province_info'] = array();
            $area_tree['province_info']['province_id'] = $province_info['id'];
            $area_tree['province_info']['province'] = $province_info['area'];
            $area_tree['province_info']['area_code'] = $area_info['area_code'];
             
            
        }
        
        return $area_tree;
        
    }
    
    /**
     * 获取省份列表
     */
    public function get_province_list()
    {
        return _model('region')->field('id,area,area_code')->getList(array('depth'=>1, 'status'=>1));
    }
    
    /**
     * 获取该省份下的城市
     * @param $province_code 省份的行政区划编号
     */
    public function get_city_list($province_code)
    {
        $province_info = _model('region')->read(array('area_code'=>$province_code));
        
        if ($province_info) {
        	return _model('region')->field('id,area,area_code')->getList(array('depth'=>2, 'status'=>1, 'parent_id'=>$province_info['area_code']));
        } else {
            return false;
        }

    }
    
    /**
     * 获取该城市下的区县列表
     * @param $city_code 城市的行政区划编码
     */
    public function get_county_list($city_code)
    {
        $city_info = _model('region')->read(array('area_code'=>$city_code));
        
        if ($city_info) {
        	return _model('region')->field('id,area,area_code')->getList(array('depth'=>3, 'status'=>1, 'parent_id'=>$city_info['area_code']));
        } else {
            return false;
        }
        
    }
    
    /**
     * 获取单条省市区的信息
     * @param 地区的行政区划代码 $area_code
     */
    public function get_area_info_by_area_code($area_code)
    {
        
        if (!$area_code) {
        	return false;
        }
        
        $area_info = _model('region')->read(array('area_code'=>$area_code));
    
        $area_tree = array();
        if (!$area_info) {
            return array();
        }
        //判断这个地区是省市区
        if ($area_info['depth'] == 1) {
            //获取省份信息
            $area_tree['province_info'] = array();
            $area_tree['province_info']['province_id'] = $area_info['id'];
            $area_tree['province_info']['area'] = $area_info['area'];
            $area_tree['province_info']['area_code'] = $area_info['area_code'];
        } elseif($area_info['depth'] == 2) {
            //获取城市信息
            $area_tree['city_info'] = array();
            $area_tree['city_info']['city_id'] = $area_info['id'];
            $area_tree['city_info']['area'] = $area_info['area'];
            $area_tree['city_info']['area_code'] = $area_info['area_code'];
    
            //获取省份信息
            $province_info = _uri('region', $area_info['parent_id']);
            $area_tree['province_info'] = array();
            $area_tree['province_info']['province_id'] = $province_info['id'];
            $area_tree['province_info']['area'] = $province_info['area'];
            $area_tree['province_info']['area_code'] = $province_info['area_code'];
             
             
        } elseif($area_info['depth'] == 3) {
            //获取区县信息
            $area_tree['county_info'] = array();
            $area_tree['county_info']['county_id'] = $area_info['id'];
            $area_tree['county_info']['area'] = $area_info['area'];
            $area_tree['county_info']['area_code'] = $area_info['area_code'];
    
            //获取城市信息
            $city_info = _uri('region', array('area_code' => $area_info['parent_id']));
            $area_tree['city_info'] = array();
            $area_tree['city_info']['city_id'] = $city_info['id'];
            $area_tree['city_info']['area'] = $city_info['area'];
            $area_tree['city_info']['area_code'] = $city_info['area_code'];
    
            //获取省份信息
            $province_info = _uri('region', array('area_code' => $city_info['parent_id']));
            $area_tree['province_info'] = array();
            $area_tree['province_info']['province_id'] = $province_info['id'];
            $area_tree['province_info']['area'] = $province_info['area'];
            $area_tree['province_info']['area_code'] = $province_info['area_code'];
             
    
        }
    
        return $area_tree;
    
    }

      
}



?>